package com.tbit.api;

import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.manager.api.TransactionOrderStatApi;
import com.tbit.manager.api.dto.DepositRefundPageDTO;
import com.tbit.manager.api.dto.OrderRefundDTO;
import com.tbit.manager.api.dto.TransactionOrderIncomeDTO;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.dto.DepositRefundDTO;
import com.tbit.uqbike.object.pojo.dto.TransactionOrderIncomePageDTO;
import com.tbit.uqbike.object.pojo.vo.TransactionOrderIncomeVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.InvoiceRecordService;
import com.tbit.uqbike.service.business.RefundService;
import com.tbit.uqbike.service.business.TransactionOrderStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * openfeign实现
 *
 * @author ：zhangtianyou
 * @since 2024/9/23
 */
@RestController
@Slf4j
public class TransactionOrderStatApiImpl implements TransactionOrderStatApi {

    @Autowired
    private RefundService refundService;

    @Autowired
    private AccountUserService accountUserService;

    @Autowired
    TransactionOrderStatService transactionOrderStatService;

    @Resource
    private InvoiceRecordService invoiceRecordService;

    @Override
    public Result<Integer> orderRefundApp(OrderRefundDTO orderRefundDTO) {
        LoginInfo loginInfo = new LoginInfo();
        com.tbit.uqbike.object.pojo.dto.OrderRefundDTO orderRefund = new com.tbit.uqbike.object.pojo.dto.OrderRefundDTO();
        AccountUser accountUser = accountUserService.getAccountUserById(orderRefundDTO.getAccountUserId());
        BeanUtils.copyProperties(orderRefundDTO, orderRefund);
        orderRefund.setRefundMoney(Long.valueOf(orderRefundDTO.getRefundMoney()));
        BeanUtils.copyProperties(accountUser, loginInfo);
        Result<Integer> integerResult = refundService.orderRefund(loginInfo, orderRefund);
        Result<Integer> result = new Result<>();
        BeanUtils.copyProperties(integerResult, result);
        return result;
    }

    @Override
    public Result<Integer> depositRefundApp(DepositRefundPageDTO depositRefundPageDTO) {
        LoginInfo loginInfo = new LoginInfo();
        DepositRefundDTO depositRefundDTO = new DepositRefundDTO();
        AccountUser accountUser = accountUserService.getAccountUserById(depositRefundPageDTO.getAccountUserId());
        BeanUtils.copyProperties(accountUser, loginInfo);
        loginInfo.setLoginUserPlatform(OperationPlatform.MAINTAIN.code);
        BeanUtils.copyProperties(depositRefundPageDTO, depositRefundDTO);
        Result<Integer> integerResult = refundService.depositRefund(loginInfo, depositRefundDTO);
        Result<Integer> result = new Result<>();
        BeanUtils.copyProperties(integerResult, result);
        return result;
    }

    @Override
    public Result<Boolean> queryOrderHasBeenInvoicedByOrderNoApp(String orderNo) {
        Result<Boolean> booleanResult = invoiceRecordService.queryOrderHasBeenInvoicedByOrderNo(orderNo);
        Result<Boolean> result = new Result<>();
        BeanUtils.copyProperties(booleanResult, result);
        return result;
    }

    @Override
    public Result<PageResult<TransactionOrderIncomeDTO>> getIncomePageApp(TransactionOrderIncomeDTO dto) {
        if (dto.getRowCount() != null && dto.getRowCount() > 1000) {
            //默认最大查询一千行
            dto.setRowCount(1000);
        }
        TransactionOrderIncomePageDTO transactionOrderIncomePageDTO = new TransactionOrderIncomePageDTO();
        PageResult<TransactionOrderIncomeDTO> result = new PageResult<>();
        BeanUtils.copyProperties(dto, transactionOrderIncomePageDTO);
        //兼容旧接口 新接口accountId参数为空
        if (dto.getAccountId() != null){
            transactionOrderIncomePageDTO.setAccountIds(dto.getAccountId());
        }else {
            transactionOrderIncomePageDTO.setAccountIds(dto.getAccountIds());
        }
        PageResult<TransactionOrderIncomeVO> incomePage = transactionOrderStatService.getIncomePage(transactionOrderIncomePageDTO);
        BeanUtils.copyProperties(incomePage, result);
        return Result.success(result);
    }
}
